<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="添加品牌广告" :visible.sync="dialogVisible" @close="resetForm('addForm')" width="400px">
        <el-form label-position="top" :model="addForm" :hide-required-asterisk='true' ref="addForm" :rules="addRules" size="medium">
            <el-form-item label="选择品牌" prop="brand_id" style="margin-bottom:0">
                <el-select v-model="addForm.brand_id" filterable placeholder="请选择品牌" style="width:100%">
                    <el-option :label="item.brand_name" :value="item.id" v-for="item in brandList" :key="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addForm')" :disabled="loading" :loading="loading" class="el-button-width">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            brandList: [],
            addForm: {
                year: "",
                month: "",
                brand_id: "",
            },
            addRules: {
                brand_id: [
                    {
                        required: true,
                        message: "请选择品牌",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {},
    methods: {
        //获取品牌列表
        async getBrandList() {
            const { data: res } = await this.$http.post(
                "launch/cost/brandListQc"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.brandList = res.data.list;
            }
        },
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "launch/cost/prjAddBrand",
                        this.addForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.$emit("getProData");
                        this.resetForm(formName);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>

</style>
